<?php
/**
 * 登陆接口
 * @author czb
 *
 */
require(dirname(dirname(__FILE__))."/includes/initInterface.php");
require(dirname(dirname(__FILE__))."/includes/cls_loginModel.php");
$action  = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'logon';
$login  = new Login();
$login->$action();
Class Login {
	private $wxid = 0;//数据库Id
	private $uId = 0;
	private $appid = "";
	private $secret = "";
	private $m;//自己的model
	private $info=array();//当前用户的微信信息
	private $jsonp;
	public function __construct(){
		
		$this->jsonp = new JSONP();
		//基础信息构建
		$this->m = new loginModel();
		$res = $this->m->get_appid_ser();
		//微信分享参数构建
		$this->appid=$res['appid'];
		$this->secret=$res['appsecret'];
	
		//当前用户的微信id
		$this->wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
	
// 		if($this->wxid>0)
// 		{
// 			//当前用户的微信信息
// 			$this->info = $this->m->get_record($this->wxid);
// 			$this->assign('info',$this->info);
// 		}
		
	}
	
	public function logon(){
		if(!isset($_SESSION['wxid'])){//空值
			if(!isset($_GET['code'])){//判断之前是否已授权，如果没有则要重新访问授权页面
				
				$url ='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
				$url = $this->parseurl($url);
				//echo 'Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
				//exit();
				header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect');
				
			}else{//已获取到code
		require(dirname(dirname(__FILE__))."/includes/weixinController.php");
				$weiXinObj= new weixinController();
				$code = $_GET['code'];
				// 获取openid 和 access_token
				$back=$weiXinObj->getUserByCode($this->appid,$this->secret,$code);
		
				$access_token=$back['access_token'];
				$openid=$back['openid'];
					
				if($openid){
		
					// 获取微信用户信息
					$arr=$weiXinObj->getUserInfo($openid,$access_token);
					$this->wxid=$this->m->is_user_existed($openid);//根据openid来获取wxid
					
					if(!$this->wxid){//是首次进入
						//构建微信用户信息
						//$arr = $this->m->buildArr($arr);
						$arr['wxid'] = $arr['openid'];
						$this->wxid = $this->m->insertUser($arr);
					}
					else
					{
						$updateDatas = array('headimgurl'=>$arr['headimgurl'],'nickname'=>$arr['nickname']);
						$this->m->updateUser($updateDatas,$this->wxid);
					}
					$this->uId = $this->m->isUser($openid);
					if(!$this->uId){//首次进入
						$this->m->insertECSUser($arr, $openid);
					}
					//授权成功，设置SESSION，并重新进入
					$_SESSION['wxid'] = $this->wxid;
				}
				header('Location:http://'.$_SERVER['HTTP_HOST'].'/interface/wx/Login.php?act=logon');
			}
		
		}else{//有值表示可以跳转
			header('Location:http://'.$_SERVER['HTTP_HOST'].'/html/chunle/index.html#/user');
		}
	}
	public function logonA(){
		if(!isset($_SESSION['wxid'])){//空值
			if(!isset($_GET['code'])){//判断之前是否已授权，如果没有则要重新访问授权页面
	
				$url ='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
				$url = $this->parseurl($url);
				//echo 'Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
				//exit();
				header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect');
	
			}else{//已获取到code
				require(dirname(dirname(__FILE__))."/includes/weixinController.php");
				$weiXinObj= new weixinController();
				$code = $_GET['code'];
				// 获取openid 和 access_token
				$back=$weiXinObj->getUserByCode($this->appid,$this->secret,$code);
	
				$access_token=$back['access_token'];
				$openid=$back['openid'];
					
				if($openid){
	
					// 获取微信用户信息
					$arr=$weiXinObj->getUserInfo($openid,$access_token);
					$this->wxid=$this->m->is_user_existed($openid);//根据openid来获取wxid
						
					if(!$this->wxid){//是首次进入
						//构建微信用户信息
						//$arr = $this->m->buildArr($arr);
						$arr['wxid'] = $arr['openid'];
						$this->wxid = $this->m->insertUser($arr);
					}
					else
					{
						$updateDatas = array('headimgurl'=>$arr['headimgurl'],'nickname'=>$arr['nickname']);
						$this->m->updateUser($updateDatas,$this->wxid);
					}
					$this->uId = $this->m->isUser($openid);
					if(!$this->uId){//首次进入
						$this->m->insertECSUser($arr, $openid);
					}
					//授权成功，设置SESSION，并重新进入
					$_SESSION['wxid'] = $this->wxid;
				}
				header('Location:http://'.$_SERVER['HTTP_HOST'].'/chunle/interface/wx/Login.php?act=logonA');
			}
	
		}else{//有值表示可以跳转
			header('Location:http://'.$_SERVER['HTTP_HOST'].'/chunle/AngularRequireJS/index.html#/user');
		}
	}
	function parseurl($url="")
	{
		$url = rawurlencode(mb_convert_encoding($url, 'gb2312', 'utf-8'));
	
		return $url;
	}
	
	public function isLogin(){
		global $_CODE;
		if($this->wxid){//有值
			//获取openid值
			$openid = $this->m->get_openid($this->wxid);
			die($this->jsonp->JSONP($_CODE['LOGIN_ISLOGIN_SUCCESS'],array("openid"=>$openid)));
		}else{
			die($this->jsonp->JSONP($_CODE['LOGIN_ISLOGIN_ERROR']));
		}
	}
	
	public function loginInfo(){
		global $_CODE;
		if($this->wxid){
			$res = $this->m->getLoginInfo($this->wxid);
			if($res){
				die($this->jsonp->JSONP($_CODE['LOGIN_LOGININFO_SUCCESS'],$res));
			}else{
				die($this->jsonp->JSONP($_CODE['LOGIN_LOGININFO_ERROR']));
			}
		}else{
			die($this->jsonp->JSONP($_CODE['SESSION_FAILED']));
		}
	}
	public function getLoginInfo(){
		global $_CODE;
		if($this->wxid){
			$res = $this->m->get_login_info($this->wxid);
			if($res){
				die($this->jsonp->JSONP($_CODE['LOGIN_LOGININFO_SUCCESS'],$res));
			}else{
				die($this->jsonp->JSONP($_CODE['LOGIN_LOGININFO_ERROR']));
			}
		}else{
			die($this->jsonp->JSONP($_CODE['SESSION_FAILED']));
		}
	}
	
	
	public function clear()
	{
		unset($_SESSION['wxid']);
		unset($_SESSION);
	}
	
	public function set()
	{
		$_SESSION['wxid']=$_GET['wxid'];
	}
	
	public function saveInfo(){
		global $_CODE;
		if($this->wxid){
			$info['user_name'] = isset($_GET['user_name'])?$_GET['user_name']:"";
			$info['mobile_phone'] = isset($_GET['mobile_phone'])?$_GET['mobile_phone']:"";
			if($info['user_name']){
				$res = $this->m->saveInfo($info,$this->wxid);
				if($res){
					die($this->jsonp->JSONP($_CODE['LOGIN_SAVEINFO_SUCCESS']));
				}else{
					die($this->jsonp->JSONP($_CODE['LOGIN_SAVEINFO_ERROR']));
				}
			}else{
				die($this->jsonp->JSONP($_CODE['LOGIN_SAVEINFO_ERROR']));
			}
		}else{
			die($this->jsonp->JSONP($_CODE['SESSION_FAILED']));
		}
	}
	
	
	
	public function commentNotify(){
	    global $_CODE;
	    if($this->wxid){
	        $info['notify'] = 0;
	        
	            $res = $this->m->saveInfo($info,$this->wxid);
	            $_SESSION['notify'] = 0;
	            die($this->jsonp->JSONP($_CODE['LOGIN_SAVEINFO_ERROR']));
	        
	    }
	}
	
}